package cn.xq.pethome.sysDictType.controller;

import cn.xq.pethome.sysDictType.domain.Dictype;
import cn.xq.pethome.sysDictType.query.DictypeQuery;
import cn.xq.pethome.sysDictType.service.IDicTypeService;
import cn.xq.pethome.utils.AjaxUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/dtype")
@Api(value ="dtype",description="产品相关的CRUD功能")
public class TypeController {
    @Autowired
    private IDicTypeService iDicTypeService;

    @GetMapping("/list")
    @ApiOperation("查询所有数据")
    public AjaxUtil findall(){
        List<Dictype> findall = iDicTypeService.findall();
        return AjaxUtil.success().setData(findall);
    }

    @PostMapping("/{id}")
    @ApiOperation("根据id查询数据")
    public AjaxUtil findone(@PathVariable("id") Long id){
        Dictype findone = iDicTypeService.findone(id);
        return AjaxUtil.success().setMsg("查找成功！").setData(findone);
    }

    @PutMapping("/")
    @ApiOperation("增加与修改")
    public AjaxUtil save(@RequestBody Dictype dictype){
        if(dictype.getId()==null){
            iDicTypeService.insert(dictype);
            return AjaxUtil.success().setMsg(("添加成功"));
        }else {
            iDicTypeService.update(dictype);
            return AjaxUtil.success().setMsg("修改成功！");
        }
    }


    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除数据")
    public AjaxUtil delete(@PathVariable("id") Long id){
        iDicTypeService.delete(id);
        return AjaxUtil.success().setMsg("删除成功！");
    }

    @PostMapping("/pagelist")
    @ApiOperation("高级查询数据")
    public  AjaxUtil findquery(@RequestBody DictypeQuery dictypeQuery){
        List<Dictype> findquery = iDicTypeService.findquery(dictypeQuery);
        Long findquerycount = iDicTypeService.findquerycount(dictypeQuery);
        return  AjaxUtil.success().setData(findquery).
                setMsg("高级查询成功！").setTotal(findquerycount);
    }

}
